package net.maku.project.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.project.convert.ProcessStepsConvert;
import net.maku.project.entity.ProcessStepsEntity;
import net.maku.project.query.ProcessStepsQuery;
import net.maku.project.vo.ProcessStepsVO;
import net.maku.project.dao.ProcessStepsDao;
import net.maku.project.service.ProcessStepsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 流程环节表
 *
 * @author aili 1106997025@qq.com
 * @since 1.0.0 2024-07-09
 */
@Service
@AllArgsConstructor
public class ProcessStepsServiceImpl extends BaseServiceImpl<ProcessStepsDao, ProcessStepsEntity> implements ProcessStepsService {

    @Override
    public PageResult<ProcessStepsVO> page(ProcessStepsQuery query) {
        IPage<ProcessStepsEntity> page = baseMapper.selectPage(getPage(query), getWrapper());

        return new PageResult<>(ProcessStepsConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<ProcessStepsEntity> getWrapper(){

        return Wrappers.lambdaQuery();
    }

    @Override
    public void save(ProcessStepsVO vo) {
        ProcessStepsEntity entity = ProcessStepsConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(ProcessStepsVO vo) {
        ProcessStepsEntity entity = ProcessStepsConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }


    @Override
    public List<Long> getBranchPoints(Long stageId) {
        List<ProcessStepsEntity> entityList = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(ProcessStepsEntity::getStageId, stageId)
                .eq(ProcessStepsEntity::getBranchPoint, 1)
                .list();
        return entityList.stream().map(ProcessStepsEntity::getId).collect(Collectors.toList());
    }
    @Override
    public String getDeadLine(Long stepId) {
        if (stepId == null){
            return null;
        }
       ProcessStepsEntity entity = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(ProcessStepsEntity::getId, stepId)
                .last("limit 1")
                .one();
       if (entity == null){
           return null;
       }
        return String.valueOf(entity.getDeadline());
    }

    @Override
    public List<Object[]> getAllStepDeadLine() {
        List<ProcessStepsEntity> entity = new LambdaQueryChainWrapper<>(baseMapper).list();
        return entity.stream().map(item -> new Object[]{item.getId(), item.getName(),item.getDeadline()}).collect(Collectors.toList());

    }

    @Override
    public String getNameById(Long stepId) {
        if (stepId == null){
            return null;
        }
       ProcessStepsEntity entity = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(ProcessStepsEntity::getId, stepId)
                .last("limit 1")
                .one();
       if (entity == null){
           return null;
       }
        return entity.getName();
    }

}